package com.example.mymessagequeue.mqserver.controller;

import com.example.mymessagequeue.common.*;
import com.example.mymessagequeue.mqserver.VirtualHost;
import com.example.mymessagequeue.mqserver.service.EagleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/eaglemq")
public class EagleController {

    @Autowired
    private EagleService eagleService;

    /**
     * 创建一个交换机
     * @param arguments
     * @return
     */
    @PostMapping("/exchange-declare")
    public R exchangeDeclare(@RequestBody ExchangeDeclareArguments arguments,
                             @RequestParam(required = false, defaultValue = VirtualHost.DEFAULT_NAME) String virtualHostName) {
        return R.success(eagleService.exchangeDeclare(arguments, virtualHostName));
    }

    /**
     * 删除一个交换机
     * @param arguments
     * @param virtualHostName
     * @return
     */
    @PostMapping("/exchange-delete")
    public R exchangeDelete(@RequestBody ExchangeDeleteArguments arguments,
                            @RequestParam(required = false, defaultValue = VirtualHost.DEFAULT_NAME) String virtualHostName) {
        return R.success(eagleService.exchangeDelete(arguments, virtualHostName));
    }

    /**
     * 查找所有的交换机
     * @return
     */
    @GetMapping("/select-all-exchanges")
    public R selectAllExchanges(@RequestParam(required = false, defaultValue = VirtualHost.DEFAULT_NAME) String virtualHostName) {
        return R.success(eagleService.selectAllExchanges(virtualHostName));
    }

    /**
     * 查找指定交换机
     * @param exchangeName
     * @param virtualHostName
     * @return
     */
    @GetMapping("/select-exchange")
    public R selectExchange(@RequestParam String exchangeName,
                            @RequestParam(required = false, defaultValue = VirtualHost.DEFAULT_NAME) String virtualHostName) {
        return R.success(eagleService.selectExchange(exchangeName, virtualHostName));
    }

    /**
     * 创建交换机
     * @param arguments
     * @param virtualHostName
     * @return
     */
    @PostMapping("/queue-declare")
    public R queueDeclare(@RequestBody QueueDeclareArguments arguments,
                          @RequestParam(required = false, defaultValue = VirtualHost.DEFAULT_NAME) String virtualHostName) {
        return R.success(eagleService.queueDeclare(arguments, virtualHostName));
    }

    /**
     * 删除指定交换机
     * @param arguments
     * @param virtualHostName
     * @return
     */
    @PostMapping("/queue-delete")
    public R queueDelete(@RequestBody QueueDeleteArguments arguments,
                         @RequestParam(required = false, defaultValue = VirtualHost.DEFAULT_NAME) String virtualHostName) {
        return R.success(eagleService.queueDelete(arguments, virtualHostName));
    }


    /**
     * 查找所有的交换机
     * @param virtualHostName
     * @return
     */
    @GetMapping("/select-all-queues")
    public R selectAllQueues(@RequestParam(required = false, defaultValue = VirtualHost.DEFAULT_NAME) String virtualHostName) {
        return R.success(eagleService.selectAllQueues(virtualHostName));
    }

    /**
     * 通过队列名查找队列
     * @param queueName
     * @param virtualHostName
     * @return
     */
    @GetMapping("/select-queue")
    public R selectQueue(@RequestParam String queueName,
                         @RequestParam(required = false, defaultValue = VirtualHost.DEFAULT_NAME) String virtualHostName) {
        return R.success(eagleService.selectQueue(queueName, virtualHostName));
    }

    /**
     * 将队列与交换机进行绑定
     * @param arguments
     * @param virtualHostName
     * @return
     */
    @PostMapping("queue-bind")
    public R queueBind(@RequestBody QueueBindArguments arguments,
                       @RequestParam(required = false, defaultValue = VirtualHost.DEFAULT_NAME) String virtualHostName) {
        return R.success(eagleService.queueBind(arguments, virtualHostName));
    }

    /**
     * 将队列与交换机解除绑定
     * @param arguments
     * @param virtualHostName
     * @return
     */
    @PostMapping("queue-unbind")
    public R queueUnbind(@RequestBody QueueUnbindArguments arguments,
                         @RequestParam(required = false, defaultValue = VirtualHost.DEFAULT_NAME) String virtualHostName) {
        return R.success(eagleService.queueUnbind(arguments, virtualHostName));
    }

    /**
     * 查询所有的绑定
     * @param virtualHostName
     * @return
     */
    @GetMapping("select-all-bindings")
    public R selectAllBindings(@RequestParam(required = false, defaultValue = VirtualHost.DEFAULT_NAME) String virtualHostName) {
        return R.success(eagleService.selectAllBindings(virtualHostName));
    }

    /**
     * 通过交换机名和队列名查找指定的绑定对象
     * @param exchangeName
     * @param queueName
     * @param virtualHostName
     * @return
     */
    @GetMapping("select-binding")
    public R selectBinding(@RequestParam String exchangeName, @RequestParam String queueName,
                           @RequestParam(required = false, defaultValue = VirtualHost.DEFAULT_NAME) String virtualHostName) {
        return R.success(eagleService.selectBinding(exchangeName, queueName, virtualHostName));
    }

    /**
     * 发布消息到指定交换机
     * @param arguments
     * @param virtualHostName
     * @return
     */
    @PostMapping
    public R basicPublish(@RequestBody BasicPublishArguments arguments,
                          @RequestParam(required = false, defaultValue = VirtualHost.DEFAULT_NAME) String virtualHostName) {
        return R.success(eagleService.basicPublish(arguments, virtualHostName));
    }

    /**
     * 通过队列名，获取这个队列上的所有消息
     * @param queueName
     * @param virtualHostName
     * @return
     */
    @GetMapping("/get-message")
    public R selectMessages(@RequestParam String queueName,
                            @RequestParam(required = false, defaultValue = VirtualHost.DEFAULT_NAME) String virtualHostName) {
        return R.success(eagleService.selectMessages(queueName, virtualHostName));
    }
}
